/**
 * Company : TIS.
 * Created By : Chatree Siangklom
 * Created On : Aug 22, 2013  12:02:03 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.impl
 * File Name : AGenCodeDaoImpl.java
 * TODO
 */
package com.tis.database.dao.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import com.tis.database.dao.AGenCodeDao;
import com.tis.database.entitys.AGenCode;

@Repository
public class AGenCodeDaoImpl extends AbstractDaoImpl<AGenCode, BigDecimal> implements AGenCodeDao {

	private static final Logger log = LoggerFactory.getLogger(AGenCodeDaoImpl.class);
	
	public AGenCodeDaoImpl() {
		super(AGenCode.class);
	}
	
	@Override	
	@SuppressWarnings("unchecked")
	public List<AGenCode> findByAGenCodeCode(String code) throws HibernateException {
		return findByCriteria(Restrictions.like("code", code, MatchMode.START));	
	}
	
	@Override	
	@SuppressWarnings("unchecked")
	public List<AGenCode> findByAGenCodeName(String name) throws HibernateException{
		return findByCriteria(Restrictions.like("name", name, MatchMode.START));
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<AGenCode> RefreshObject(List<AGenCode> lst) throws HibernateException {
		try {
			
			Session session = getCurrentSession();
			
			String strValue = "";
			int iRow = CommonService.getSize(lst); 
			for(int i = 0; i < iRow; i++) {
				if(i > 0)
					strValue += "," + Utilitys.strSql(CommonService.IsNullRString(lst.get(i).getPkId()));
				else //First loop
					strValue = Utilitys.strSql(CommonService.IsNullRString(lst.get(i).getPkId()));			
			}
			
			Query query = session.getNamedQuery(AGenCode.findInID).setString("pkId", strValue).setString("p_orderby", "ACS");
			
			if(CommonService.getSize(query.list()) > 0 )			
				return Collections.checkedList(query.list(), AGenCode.class);
			else 
				return null;
			
		} catch (HibernateException e) {
			throw e;
		}
	}
}